home *** CD-ROM | disk | FTP | other *** search
- Path: hobbes.cc.uga.edu!news
- From: Andy Dustman <andy@CCMSD.chem.uga.edu>
- Newsgroups: comp.lang.c++
- Subject: Template troubles with partial solution (?)
- Date: Wed, 03 Apr 1996 23:49:55 -0500
- Organization: University of Georgia, Athens
- Message-ID: <316354F3.7DEA1F8@CCMSD.chem.uga.edu>
- NNTP-Posting-Host: neptune.chem.uga.edu
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; Linux 1.3.80 i586)
- CC: andy@ccmsd.chem.uga.edu
-
- I'm trying to modify an existing library of matrix routines into a template
- and I'm having lots of trouble. I'm using G++ 2.7.0, but 2.7.2 seems to have
- the same problems.
-
- The original looks something like this:
-
- matrix.h:
-
- class matrix {
- double* mat;
- ...
- matrix& operator+= (const matrix& m) ;
- ...
- } ;
- ...
- matrix operator+ (const matrix& m, const matrix& n);
-
- *********
- matrix.c:
-
- #include "matrix.h"
-
- matrix& matrix::operator+= (const matrix& m)
- { ... }
-
- matrix operator + (const matrix& m, const matrix& n)
- {
- matrix p = m;
- p += n;
- return p;
- }
-
-
- This compiles and works well. However, I would like to turn it into a
- template so I could use matrix<double> or matrix<complex>. So: I did
- s/double/T/ and s/matrix/matrix<T>/ throughout the source tree, plus some
- other editing and come up with this:
-
- matrix.h:
-
- template<class T>
- class matrix {
- T* mat;
- ...
- matrix<T>& operator+= (const matrix<T>& m);
- ...
- } ;
- template<class T> matrix<T> operator+ (const matrix<T>& m, const matrix<T>& n); \\ A
-
- *********
- matrix.c:
-
- #include "matrix.h"
-
- template<class T> matrix<T> matrix<T>::operator+= (const matrix<T>& m);
- { ... }
-
- template<class T> matrix<T> operator + (const matrix& m, const matrix& n) \\ B
- {
- matrix<T> p = m;
- p += n;
- return p;
- }
-
- **********
- dmatrix.c:
-
- #include "matrix.c"
-
- template class matrix<double>;
-
-
- This compiles, but at link time, I get:
-
- test.o(.text+0x14d): undefined reference to `operator+(matrix<double> const &, matrix<double> const &)'
-
- Okay, I figure, I'll just prepend "matrix<T>::" to "operator +" on line B
- above and that'll fix it, right? Uh-uh:
-
- matrix.h:(end of template): method `operator +' not found in class `matrix<double>'
- matrix.c:B: `matrix<...>::operator +(matrix<...> &, matrix<...> &)' must take either zero or one argument
-
- Now this is a pretty strange error, so I figure the obvious thing is to
- move line A up into the template and modify it to look like the += def:
-
- matrix<T>& operator+= (const matrix<T>& m);
- matrix<T> operator+ (const matrix<T>& m, const matrix<T>& n);
-
- I try this. It still insists on "either zero or one argument". So I oblige.
-
- matrix<T> operator+ (const matrix<T>& n);
-
- and change matrix.c:
-
- template<class T> matrix<T> operator + (const matrix& n) \\ B
- {
- matrix<T> p = *this;
- p += n;
- return p;
- }
-
- This compiles, and it seems to resolve the symbol problems. (I actually
- have quite a few more to fix, but I gotta go home!) (Yes, I worked this
- last part out writing this!) But the question remains:
-
- In the original version, the operator+ is declared outside of the class
- definition, but in the template version, it seems to have to be declared
- within the class definition. Is this really how things should work, or
- did I miss something? If you don't know by now, I am just learning C++.
- I've got Stroustrup's "C++ PL, 2nd ed., reprint April 95", so any references
- to that would be useful. No, I haven't read the whole thing yet...
-
- --
- Andy Dustman / Computational Center for Molecular Structure and Design / UGA
- ===== For PGP public key: finger andy@neptune.chem.uga.edu | pgp -fka =====
- Sure, the Telecomm Act will create jobs: 100,000 new thought-cops on the net
- http://charon.chem.uga.edu/~andy mailto:andy@CCMSD.chem.uga.edu <}+++<
-